package com.jeesite.modules.fl.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.fl.entity.Enterprise;
import com.jeesite.modules.fl.service.EnterpriseService;

@Controller
@RequestMapping(value = "${adminPath}/fl/enterprise")
public class EnterpriseController extends BaseController{
	
	@Autowired
	private EnterpriseService enterpriseService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Enterprise get(String id, boolean isNewRecord) {
		return enterpriseService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("fl:enterprise:view")
	@RequestMapping(value = {"list", ""})
	public String list(Enterprise enterprise, HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("enterprise", enterprise);
		return "modules/fl/enterpriseList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("fl:enterprise:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Enterprise> listData(Enterprise enterprise, HttpServletRequest request, HttpServletResponse response) {
		Page<Enterprise> page = enterpriseService.findPage(new Page<Enterprise>(request, response), enterprise);
		return page;
	}
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("fl:enterprise:view")
	@RequestMapping(value = "form")
	public String form(Enterprise enterprise, Model model) {
		model.addAttribute("enterprise", enterprise);
		return "modules/fl/enterpriseForm";
	}
	
	/**
	 * 验证名称是否有效
	 * @param oldEnterpriseName
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequiresPermissions("fl:enterprise:edit")
	@RequestMapping(value = "checkEnterpriseName")
	public String checkEnterpriseName(String oldEnterpriseName, String name) {
		if (name !=null && name.equals(oldEnterpriseName)) {
			return "true";
		} else if (name !=null && enterpriseService.getByName(name) == null) {
			return "true";
		}
		return "false";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("fl:enterprise:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Enterprise enterprise) {
		enterpriseService.save(enterprise);
		return renderResult(Global.TRUE, text("保存数据成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("fl:enterprise:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Enterprise enterprise) {
		enterprise.setStatus(Enterprise.STATUS_DISABLE);
		enterpriseService.updateStatus(enterprise);
		return renderResult(Global.TRUE, text("停用数据成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("fl:enterprise:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Enterprise enterprise) {
		enterprise.setStatus(Enterprise.STATUS_NORMAL);
		enterpriseService.updateStatus(enterprise);
		return renderResult(Global.TRUE, text("启用数据成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("fl:enterprise:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Enterprise enterprise) {
		enterpriseService.delete(enterprise);
		return renderResult(Global.TRUE, text("删除数据成功！"));
	}
	
	@RequestMapping(value = "import/enterpriseTemplate")
	public void importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		ExcelExport ee = new ExcelExport("企业数据", Enterprise.class);
		try {
			String fileName = "企业数据导入模板.xlsx";
			List<Enterprise> list = new ArrayList<Enterprise>();
			ee.setDataList(list).write(response, fileName).close();
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		} finally{
			ee.close();
		}
	}
	
	@RequestMapping(value = "export", method=RequestMethod.POST)
    public void exportFile(Enterprise enterprise, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		ExcelExport ee = new ExcelExport("企业数据", Enterprise.class);
		try {
            String fileName = "企业数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx"; 	
            List<Enterprise> list = enterpriseService.findList(enterprise);
            System.out.println(list);
    		ee.setDataList(list).write(response, fileName);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出企业失败！失败信息："+e.getMessage());
		} finally{
			ee.close();
		}
    }
    
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importData(MultipartFile file, String updateSupport)
    {
        String message = "";
        boolean isUpdateSupport = "1".equals(updateSupport);
        try{
	        message = enterpriseService.importData(file, Boolean.valueOf(isUpdateSupport));
	        return renderResult("true", (new StringBuilder()).append("posfull:").append(message).toString());
        }catch(Exception ex){
        	return renderResult("false", (new StringBuilder()).append("posfull:").append(ex.getMessage()).toString());
        }
    }

}
